package com.jandan.persistence.sqlmapdao;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.jandan.persistence.iface.LessonDao;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.Lesson;
import com.jandan.ui.model.ReviewInfo;


/**
 * LessonDao接口实现类，使用Ibatis框架进行持久化
 * @author Gong Yong
 * @version 1.0
 * @see com.jandan.persistence.iface.LessonDao
 */
public class LessonSqlMapDao extends SqlMapClientDaoSupport implements
		LessonDao {

	public Lesson getLessonByLessonID(long lessonID) {
		// TODO Auto-generated method stub
		return (Lesson) this.getSqlMapClientTemplate().queryForObject(
				"Lesson.getLessonByLessonID", Long.valueOf(lessonID));
	}

	public List<Lesson> getLessonListByUserID(long userID) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList(
				"Lesson.getLessonListByUserID", userID);
	}

	public int getLessonsCountByWordLibID(long wordLibID) {
		// TODO Auto-generated method stub
		return ((Integer) this.getSqlMapClientTemplate().queryForObject(
				"Lesson.getLessonsCountByWordLibID", Long.valueOf(wordLibID)))
				.intValue();
	}

	public long insertLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		long lessonID = 0;
		try {
			this.getSqlMapClient().startTransaction();
			this.getSqlMapClientTemplate()
					.insert("Lesson.insertLesson", lesson);

			lessonID = (Long) getSqlMapClientTemplate().queryForObject(
					"Lesson.lastInsertLessonID");
			this.getSqlMapClient().commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return lessonID;
	}

	
	public void updateLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("Lesson.updateLesson", lesson);
	}

	public void deleteLesson(long lessonID) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("Lesson.deleteLesson",
				Long.valueOf(lessonID));
	}

	public List<ReviewInfo> getReviewInfoListByToday(long lessonID) {
		// TODO Auto-generated method stub
		List<ReviewInfo> reviewInfoList = this.getSqlMapClientTemplate().queryForList("LearnedWord.getReviewInfoListByToday", lessonID);
		
		return reviewInfoList;
	}

	public ReviewInfo getReviewInfoOfSomeDay(long lessonID,Date date) {
		// TODO Auto-generated method stub
		//找到startLearnedDate与date在同一天的单词
		Map map = new HashMap();
		map.put("lessonID",lessonID);
		map.put("date",date);
		
		ReviewInfo reviewInfo = (ReviewInfo)this.getSqlMapClientTemplate().queryForObject("LearnedWord.getReviewInfoOfSomeDay",map);
		return reviewInfo;
	}

	public ReviewInfo getTodayReviewInfo(long lessonID) {
		// TODO Auto-generated method stub
		//读取startlearneddate为今天的单词
		Date date = new Date();
		
		return this.getReviewInfoOfSomeDay(lessonID,date);
	}

	public int getTotalLearnedWordCount(long lessonID) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		map.put("lessonID", lessonID);
		map.put("degree", 0);
		return (Integer)this.getSqlMapClientTemplate().queryForObject("LearnedWord.getWordCountByDegree",map);
	}

	public int getTotalMasterWordCount(long lessonID) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		map.put("lessonID", lessonID);
		map.put("degree", ClientUtil.MASTER);
		return (Integer)this.getSqlMapClientTemplate().queryForObject("LearnedWord.getWordCountByDegree",map);
	}
	
	public int getTotalKnowWordCount(long lessonID) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		map.put("lessonID", lessonID);
		map.put("degree", ClientUtil.KNOW);

		return (Integer)this.getSqlMapClientTemplate().queryForObject("LearnedWord.getWordCountByDegree",map);
	}

	public int getTotalUnknowWordCount(long lessonID) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		map.put("lessonID", lessonID);
		map.put("degree", ClientUtil.UNKNOW);

		return (Integer)this.getSqlMapClientTemplate().queryForObject("LearnedWord.getWordCountByDegree",map);
	}
}
